home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / jabber / filetransfer / StreamInitiation.pyo (.txt) < prev   
Python Compiled Bytecode  |  2008-10-13  |  10KB  |  262 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from jabber.objects.si_filetransfer import File
  6. from common.filetransfer import IncomingFileTransfer
  7. from jabber.objects.si_filetransfer import Feature
  8. from jabber.objects.si_filetransfer import SI_FileTransfer
  9. from jabber.objects.si import SI_NS
  10. from pyxmpp.error import STANZA_ERROR_NS
  11. from pyxmpp.all import Iq
  12. from util.primitives import lock
  13. from jabber.filetransfer.initiateStream import if_not_done, done_states
  14. from path import path
  15. import threading
  16. from logging import getLogger
  17. log = getLogger('jabber.ft.si')
  18.  
  19. class StreamInitiationHandler(object):
  20.     
  21.     def __init__(self, jabber_object):
  22.         self.j = jabber_object
  23.         self.handlers = { }
  24.         self.supported_streams = { }
  25.  
  26.     
  27.     def set_stream_handler(self, namespace, handler):
  28.         self.supported_streams[namespace] = handler
  29.  
  30.     
  31.     def set_profile_handler(self, namespace, handler):
  32.         self.handlers[namespace] = handler
  33.  
  34.     
  35.     def register_handlers(self):
  36.         self.j.stream.set_iq_set_handler('si', SI_NS, self.incoming)
  37.  
  38.     
  39.     def incoming(self, stanza):
  40.         prof = stanza.xpath_eval('si:si/@profile', {
  41.             'si': SI_NS })[0].content
  42.         if prof not in self.handlers:
  43.             self.respond_not_understood(stanza)
  44.         else:
  45.             self.handlers[prof](self, Iq(stanza.get_node()))
  46.  
  47.     
  48.     def respond_no_valid(self, stanza):
  49.         e = stanza.make_error_response('bad-request')
  50.         n = e.xpath_eval('si:si', {
  51.             'si': SI_NS })[0]
  52.         n.unlinkNode()
  53.         n.freeNode()
  54.         e.set_from(None)
  55.         er = e.xpath_eval('ns:error')[0]
  56.         er.newProp('code', '400')
  57.         c = er.newChild(None, 'no-valid-streams', None)
  58.         ns = c.newNs(SI_NS, None)
  59.         c.setNs(ns)
  60.         t = e.xpath_eval('ns:error/@type')[0]
  61.         t.setContent('cancel')
  62.         self.j.send(e)
  63.  
  64.     
  65.     def respond_not_understood(self, stanza):
  66.         e = stanza.make_error_response('bad-request')
  67.         n = e.xpath_eval('si:si', {
  68.             'si': SI_NS })[0]
  69.         n.unlinkNode()
  70.         n.freeNode()
  71.         e.set_from(None)
  72.         er = e.xpath_eval('ns:error')[0]
  73.         er.newProp('code', '400')
  74.         c = er.newChild(None, 'bad-profile', None)
  75.         ns = c.newNs(SI_NS, None)
  76.         c.setNs(ns)
  77.         self.j.send(e)
  78.  
  79.     
  80.     def reject(self, stanza, reason = None):
  81.         e = stanza.make_error_response('forbidden')
  82.         n = e.xpath_eval('si:si', {
  83.             'si': SI_NS })[0]
  84.         n.unlinkNode()
  85.         n.freeNode()
  86.         e.set_from(None)
  87.         er = e.xpath_eval('ns:error')[0]
  88.         er.newProp('code', '403')
  89.         if not reason:
  90.             pass
  91.         c = er.newChild(None, 'text', 'Offer Declined')
  92.         ns = c.newNs(STANZA_ERROR_NS, None)
  93.         c.setNs(ns)
  94.         self.j.send(e)
  95.  
  96.     
  97.     def send_accept(self, stanza, payload):
  98.         i = stanza.make_result_response()
  99.         payload.as_xml(i.get_node())
  100.         self.j.send(i)
  101.  
  102.     
  103.     def result_accept(self):
  104.         pass
  105.  
  106.     
  107.     def result_error(self):
  108.         pass
  109.  
  110.  
  111.  
  112. class FileTransferSIHandler(IncomingFileTransfer):
  113.     
  114.     def __init__(self, si_handler, iq):
  115.         self.si_handler = si_handler
  116.         self.stanza = iq
  117.         self.si_ft = SI_FileTransfer.from_iq(iq)
  118.         self.bytestream = None
  119.         self._lock = threading.RLock()
  120.         if self.check_streams():
  121.             ft = self.si_ft
  122.             self.numfiles = 1
  123.             self.name = ft.file.name
  124.             self.size = ft.file.size
  125.             self.buddy = self.si_handler.j.buddies[iq.get_from()]
  126.             file_desc = unicode(ft.file.desc)
  127.             IncomingFileTransfer.__init__(self)
  128.             si_handler.j.hub.on_file_request(si_handler.j, self)
  129.             self.on_get_buddy(self.buddy)
  130.         
  131.  
  132.     
  133.     def protocol(self):
  134.         return self.si_handler.j
  135.  
  136.     protocol = property(protocol)
  137.     
  138.     def file_desc(self):
  139.         return unicode(self.si_ft.file.desc)
  140.  
  141.     
  142.     def decline(self, reason = None):
  143.         self.si_handler.reject(self.stanza, reason)
  144.         self.state = self.states.CANCELLED_BY_YOU
  145.         IncomingFileTransfer.decline(self)
  146.  
  147.     
  148.     def cancel(self, state = None):
  149.         if state is None:
  150.             state = self.states.CANCELLED_BY_YOU
  151.         
  152.         self.state = state
  153.         if self.bytestream is not None:
  154.             self.bytestream.close()
  155.         
  156.  
  157.     cancel = lock(if_not_done(cancel))
  158.     
  159.     def accept(self, _openfileobj):
  160.         self.state = self.states.CONNECTING
  161.         for stream_type in self.si_handler.supported_streams:
  162.             if stream_type in self.si_ft.feature.possible_streams:
  163.                 selected = stream_type
  164.                 break
  165.                 continue
  166.         
  167.         self._fileobj = _openfileobj
  168.         self.filepath = path(_openfileobj.name)
  169.         si_ft = SI_FileTransfer(self.si_ft.sid)
  170.         si_ft.feature = Feature(selected_stream = selected)
  171.         si_ft.file = File()
  172.         self.bytestream = b = self.si_handler.supported_streams[selected](self.stanza)
  173.         b.bind('stream_connected', self.transferring)
  174.         b.bind('stream_closed', self.stream_closed)
  175.         b.bind('stream_connect_failed', self.stream_connect_failed)
  176.         b.bind('stream_data_recieved', self.incoming_data)
  177.         b.bind('stream_error', self.stream_error)
  178.         self.si_handler.send_accept(self.stanza, si_ft)
  179.  
  180.     
  181.     def transferring(self):
  182.         self.state = self.states.TRANSFERRING
  183.  
  184.     transferring = lock(if_not_done(transferring))
  185.     
  186.     def stream_connect_failed(self):
  187.         self.state = self.states.CONN_FAIL
  188.         self.cleanup()
  189.  
  190.     stream_connect_failed = lock(if_not_done(stream_connect_failed))
  191.     
  192.     def stream_error(self):
  193.         self.state = self.states.CONN_FAIL_XFER
  194.         self.cleanup()
  195.  
  196.     stream_error = lock(if_not_done(stream_error))
  197.     
  198.     def cleanup(self):
  199.         
  200.         try:
  201.             self._fileobj.close()
  202.         except:
  203.             pass
  204.  
  205.         self.unbind_all()
  206.         if self.state != self.states.CANCELLED_BY_YOU:
  207.             self.on_error()
  208.         
  209.  
  210.     
  211.     def stream_closed(self):
  212.         if self.completed == self.si_ft.file.size:
  213.             
  214.             try:
  215.                 self._fileobj.close()
  216.             except:
  217.                 pass
  218.  
  219.             self.unbind_all()
  220.             self._ondone()
  221.         else:
  222.             log.info('%r: Stream closed. Setting state to CANCELLED_BY_BUDDY and cleaning up.')
  223.             self.state = self.states.CANCELLED_BY_BUDDY
  224.             self.cleanup()
  225.  
  226.     stream_closed = lock(if_not_done(stream_closed))
  227.     
  228.     def unbind_all(self):
  229.         b = self.bytestream
  230.         b.unbind('stream_connected', self.transferring)
  231.         b.unbind('stream_closed', self.stream_closed)
  232.         b.unbind('stream_connect_failed', self.stream_connect_failed)
  233.         b.unbind('stream_data_recieved', self.incoming_data)
  234.  
  235.     
  236.     def check_streams(self):
  237.         if set(self.si_ft.feature.possible_streams) & set(self.si_handler.supported_streams):
  238.             return True
  239.         else:
  240.             self.si_handler.respond_no_valid(self.stanza)
  241.             return False
  242.  
  243.     
  244.     def incoming_data(self, data):
  245.         
  246.         try:
  247.             self._fileobj.write(data)
  248.             self._setcompleted(len(data) + self.completed)
  249.         except ValueError:
  250.             self._lock.__enter__()
  251.             
  252.             try:
  253.                 pass
  254.             finally:
  255.                 pass
  256.  
  257.         except:
  258.             self._lock
  259.  
  260.  
  261.  
  262.